<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/4/28
 * Time: 15:09
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Chat extends Base
{
    /**
     * 单聊记录
     * @return mixed|\think\response\Json
     */
    public function chatIndex(){
        if(Request::instance()->isAjax()){
            $user_phone1 = input('phone1');
            $user_phone2 = input('phone2');
            if(!$user_phone1 && !$user_phone2) return json(['code' => 100, 'msg' => '请完善搜索条件']);
            $list = Db::table('up_user_chat')
                ->where(function($query) use($user_phone1, $user_phone2){
                    $query->where(['chat_send_phone' => $user_phone1, 'chat_receive_phone' => $user_phone2]);
                })
                ->whereOr(function ($query1) use ($user_phone1, $user_phone2){
                    $query1->where(['chat_send_phone' => $user_phone2, 'chat_receive_phone' => $user_phone1]);
                })
                ->field('chat_send_phone, chat_content, chat_type, chat_time')
                ->order('chat_time asc')
                ->select();
            if($list){
                return json(['code' => 200, 'msg' => $list]);
            }else{
                return json(['code' => 200, 'msg' => []]);
            }
        }
        return $this->fetch('chat/chatindex');
    }
}